/**
 * 
 */
package com.scpii.universal.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.AppViewContentRalationTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.SQLs;

/**
 * UniversalApi
 * 
 * @author 甘建新 2012-8-7下午01:33:43
 */
public class AppViewContentRelationTabService implements
		BaseInterface<AppViewContentRalationTab> {
//	BaseDao<AppViewContentRalationTab> appViewContentRelationDao = SpringBeanFactory.appViewContentRelationTabDao;
	BaseDao<AppViewContentRalationTab> appViewContentRelationDao;
	public AppViewContentRalationTab get(Integer id) throws Exception {
		return appViewContentRelationDao.get(id);
	}

	public List<AppViewContentRalationTab> getAll() throws Exception {
		return appViewContentRelationDao.getAll();
	}

	public AppViewContentRalationTab get(String propertyName, Object value)
			throws Exception {
		return appViewContentRelationDao.get(propertyName, value);
	}

	public List<AppViewContentRalationTab> getList(String propertyName,
			Object value) throws Exception {
		return appViewContentRelationDao.getList(propertyName, value);
	}

	public Long getTotalCount() throws Exception {
		return appViewContentRelationDao.getTotalCount();
	}

	public void save(AppViewContentRalationTab obj) throws Exception {
		appViewContentRelationDao.save(obj);
	}

	public void update(AppViewContentRalationTab obj) throws Exception {
		appViewContentRelationDao.update(obj);
	}

	public void delete(AppViewContentRalationTab obj) throws Exception {
		appViewContentRelationDao.delete(obj);
	}

	public void delete(Integer id) throws Exception {
		appViewContentRelationDao.delete(id);
	}

	public List<AppViewContentRalationTab> hqlGetList(String hql,
			Map<String, Object> argsMap) throws Exception {
		return appViewContentRelationDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		appViewContentRelationDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return appViewContentRelationDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<AppViewContentRalationTab> sqlGetList(String sqls,
			Map<String, Object> argsMap) throws Exception {
		return appViewContentRelationDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		appViewContentRelationDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return appViewContentRelationDao.sqlGetNum(sqls, argsMap);
	}
	
	/**
	 * 获得内容视图关系的列表
	 * @param viewId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-7下午04:23:20
	 */
	public List<AppViewContentRalationTab> getAppViewContentList(int viewId)throws Exception{
		if (viewId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("viewId", viewId);
			List<AppViewContentRalationTab> appViewContentList = this.hqlGetList("SELECT_VIEW_CONTENT_BYVIEWID", argsMap);
			return appViewContentList;
		}
		return null;
	}
	
	/**
	 * 获得记录页数
	 * @param viewId
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-10下午04:59:36
	 */
	public Integer returnPageCountByViewId(int viewId, int appId,long userId)throws Exception {
		if (viewId>0) {
			List<AppViewContentRalationTab> appViewContRList = this.getAppViewContentList(viewId);
			if (appViewContRList == null || appViewContRList.size() <= 0) {
				return 0;
			}
			int result = appViewContRList.size();
			if (result == 1) {
				//只绑定了一条数据，判断是否绑定的为整个分类下的内容列表
				AppViewContentRalationTab appViewContentRTab = appViewContRList.get(0);
				if (appViewContentRTab.getIntBindType() == Const.VIEW_BIND_CATELOG) {
					//根据分类ID查询内容条数
					Map<String, Object> argsMap = new HashMap<String, Object>();
					argsMap.put("catelogId", appViewContentRTab.getIntContentId());
					argsMap.put("appId", appId);
					Long resultCount = this.hqlGetNums("GET_CONTENT_PAGENO_BY_CATELOGID", argsMap);
					result = resultCount.intValue();
				}
			}
			//计算页数并返回
			int pageCount = appViewContentRelationDao.getRecordPageNum(result, Const.MOBILE_CLIENT_NO_PERPAGE);
			return pageCount;
		}
		return 0;
	}

	/**
	 * 根据指定页面，获取指定页面中的内容
	 * @param pageNum
	 * @param viewId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-10下午05:10:40
	 */
	public List<AppViewContentRalationTab> getAppViewContentRList(int pageNum,int viewId)throws Exception{
		int MAXROWNUM = pageNum * Const.MOBILE_CLIENT_NO_PERPAGE;
		int MINROWNUM = (pageNum-1) * Const.MOBILE_CLIENT_NO_PERPAGE;
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("MAXROWNUM", MAXROWNUM);
		argsMap.put("MINROWNUM", MINROWNUM);
		argsMap.put("viewId", viewId);
		List<AppViewContentRalationTab> appViewContentRList = this.sqlGetList(SQLs.GET_VIEW_CONTENT_RELATION, argsMap);
		return appViewContentRList;
	}
	// Setter
	public void setAppViewContentRelationDao(
			BaseDao<AppViewContentRalationTab> appViewContentRelationDao) {
		this.appViewContentRelationDao = appViewContentRelationDao;
	}

	public static void main(String[] args) {
		AppViewContentRelationTabService appContService = new AppViewContentRelationTabService();
		try {
			List<AppViewContentRalationTab> appContList = appContService.getAppViewContentList(478);
			System.out.println("查询结果数为："+appContList.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
